package org.demo.ui;

import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

import org.demo.utils.Config;

public abstract class BaseFrame extends JFrame {

	private static final long serialVersionUID = -8114224965653744973L;
	protected static final String TITLE = "Qualcomm Image Read Back Tool";
	protected static final String TITLE_HELP = "Help Topic";
	protected static final String TITLE_CONFIGURATION = "Configuration";
	protected static final String TITLE_ABOUT = "About";
	protected static final String FONT_TIMES_NEW_ROMAN = "Times New Roman";
	protected static final String FONT_CONSOLAS = "Consolas";
	protected static final String FONT_DEJAVU_SANS_MOMO = "DejaVu Sans Mono";
	protected static final String FONT_ARIAL = "Arial";
	protected static final String FONT_SONGTI = "宋体";
	protected static final String FONT_YANHEI = "微软雅黑";
	protected static final String DEFAULT_FONT = FONT_YANHEI;
	protected Font defaultFont = new Font(DEFAULT_FONT, Font.PLAIN, 12);
	protected Font titleFont = new Font(DEFAULT_FONT, Font.BOLD, 14);

	protected Config mSettingConfig;

	public BaseFrame() {
		initGlobalFont(defaultFont);
		setResizable(false);
		initSettingConfig();
		initConfig();
		initLayout();
	}

	protected void initConfig() {
		
	}

	protected void initSettingConfig() {
		File file = new File("setting.ini");
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		mSettingConfig = new Config(file.getAbsolutePath());
	}

	protected abstract void initLayout();
	
	protected void initGlobalFont(Font font) {
		FontUIResource fontRes = new FontUIResource(font);
		for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof FontUIResource) {
				UIManager.put(key, fontRes);
			}
		}
		UIManager.put("Menu.font", font);
		UIManager.put("MenuItem.font", font);
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected String getQSaharaServer() {
		String path = mSettingConfig.get(Config.KEY_QSAHARASERVER);
		if (path == null || path.isEmpty()) {
			File file = new File("QSaharaServer.exe");
			if (file.exists()) {
				return file.getAbsolutePath();
			}
			return Config.DEFAULT_QSAHARASERVER;
		}
		return path;
	}

	protected String getFireHoseLoader() {
		String path = mSettingConfig.get(Config.KEY_FIREHOSELOADER);
		if (path == null || path.isEmpty()) {
			File file = new File("fh_loader.exe");
			if (file.exists()) {
				return file.getAbsolutePath();
			}
			return Config.DEFAULT_FIREHOSELOADER;
		}
		return path;
	}
}
